package com.ay277.htqoa.event.dao;

import com.ay277.htqoa.base.dao.BaseDao;
import com.ay277.htqoa.event.entity.Event;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.query.Query;
import org.hibernate.transform.Transformers;
import org.springframework.orm.hibernate5.HibernateCallback;
import org.springframework.stereotype.Repository;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/12/19.
 */
@Repository
public class EventDaoImpl extends BaseDao<Event,String> implements EventDao {
    {
        type = Event.class;
    }

    @Override
    public List<Event> select(final String start, final String end) {
        return getHibernateTemplate().executeWithNativeSession(new HibernateCallback<List<Event>>() {
            @Override
            public List<Event> doInHibernate(Session session) throws HibernateException {
                Query query=session.createQuery("select e.id as id,e.title as title,e.start as start,e.end as end,content as content from Event e where e.start>=:start and e.end <= :end");
                SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
                try {

                    Date s=format.parse(start);
                    Date e=format.parse(end);
                    System.out.println(s+"\t"+e);
                    query.setParameter("start", s);
                    query.setParameter("end", e);
                }catch (Exception e){
                    e.printStackTrace();
                }
                query.setResultTransformer(Transformers.aliasToBean(Event.class));
                return query.list();
            }
        });
    }
}






